package com.github.phoenix.widget;

import com.github.phoenix.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * 防京东密码键盘
 *
 * @author Phoenix
 * @date 2016-10-9 11:06
 */
public class Keyboard extends DependentLayout {
    private Context context;
    private ListContainer gvKeyboard;

    private String[] key;
    private OnClickKeyboardListener onClickKeyboardListener;

    public Keyboard(Context context) {
        this(context, null);
    }

    public Keyboard(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    public Keyboard(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    /**
     * 初始化KeyboardView
     */
    private void initKeyboardView() {
        Component view = LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_view_keyboard, null, false);
        addComponent(view, new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT));

        gvKeyboard = (ListContainer) view.findComponentById(ResourceTable.Id_gv_keyboard);
        TableLayoutManager layoutManager = new TableLayoutManager();
        layoutManager.setColumnCount(3);
        layoutManager.setOrientation(Component.HORIZONTAL);
        gvKeyboard.setLayoutManager(layoutManager);
        gvKeyboard.setItemProvider(keyboardAdapter);
    }

    public interface OnClickKeyboardListener {
        void onKeyClick(int position, String value);
    }

    /**
     * 对外开放的方法
     *
     * @param onClickKeyboardListener
     */
    public void setOnClickKeyboardListener(OnClickKeyboardListener onClickKeyboardListener) {
        this.onClickKeyboardListener = onClickKeyboardListener;
    }

    /**
     * 设置键盘所显示的内容
     *
     * @param key key
     */
    public void setKeyboardKeys(String[] key) {
        this.key = key.clone();
        initKeyboardView();
    }

    private BaseItemProvider keyboardAdapter = new BaseItemProvider() {
        private static final int KEY_NINE = 9;

        @Override
        public int getCount() {
            return key.length;
        }

        @Override
        public Object getItem(int position) {
            return key[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component convertView, ComponentContainer componentContainer) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                if (getItemViewType(position) == 1) {
                    //数字键
                    convertView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_grid_keyboard, componentContainer, false);
                } else {
                    //删除键
                    convertView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_grid_keyboard_delete, componentContainer, false);
                }
                viewHolder = new ViewHolder(convertView);
            }

            viewHolder.initClick(position);

            if (getItemViewType(position) == 1) {
                viewHolder = (ViewHolder) convertView.getTag();
                viewHolder.initText();
                viewHolder.tvKey.setText(key[position]);
            }

            convertView.setWidth(getDisplayWidthInPx() / 3);
            return convertView;
        }

        public int getItemViewType(int position) {
            return (getItemId(position) == KEY_NINE) ? 2 : 1;
        }


    };

    /**
     * ViewHolder,view缓存
     */
    class ViewHolder {
        private Text tvKey;
        private int position;
        private Component view;

        public ViewHolder(Component view) {
            this.view =view;
            view.setTag(this);
        }

        public void initText() {
            tvKey = (Text) view.findComponentById(ResourceTable.Id_tv_keyboard_keys);
        }

        public void initClick(int p) {
            this.position = p;
            view.setClickedListener(new ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (onClickKeyboardListener != null && position >= 0) {
                        onClickKeyboardListener.onKeyClick(position, key[position]);
                    }
                }
            });
        }
    }

    /**
     * 获取屏幕宽度
     *
     * @return 屏幕宽度
     */
    private int getDisplayWidthInPx() {
        Display display = DisplayManager.getInstance().getDefaultDisplay(getContext()).get();
        return display.getAttributes().width;
    }

}
